package com.zb.kc.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zb.kc.commonutils.R;
import com.zb.kc.statistics.client.UcenterClient;
import com.zb.kc.statistics.entity.StatisticsDaily;
import com.zb.kc.statistics.dao.StatisticsDailyDao;
import com.zb.kc.statistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author wanggen
 * @since 2020-04-25
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyDao, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        // 添加记录之前删除表相同日期的数据
        baseMapper.delete(new QueryWrapper<StatisticsDaily>().eq("date_calculated", day));
        R registerR = ucenterClient.countRegister(day);
        // 获取注册的人数
        Integer count = (Integer) registerR.getData().get("item");
        // 创建static对象
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(count); // 注册人数
        sta.setDateCalculated(day); // 统计的日期
        sta.setVideoViewNum(RandomUtils.nextInt(100, 200));//TODO
        sta.setLoginNum(RandomUtils.nextInt(100, 200));//TODO
        sta.setCourseNum(RandomUtils.nextInt(100, 200));//TODO
        // 把数据添加到数据库
        baseMapper.insert(sta);
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated", type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);
        // 日期和对应的数量
        List<String> dateList = staList.stream().map(StatisticsDaily::getDateCalculated).collect(Collectors.toList());
        List<Integer> countList = null;

        switch (type) {
            case "login_num":
                countList = staList.stream().map(StatisticsDaily::getLoginNum).collect(Collectors.toList());
                break;
            case "register_num":
                countList = staList.stream().map(StatisticsDaily::getRegisterNum).collect(Collectors.toList());
                break;
            case "video_view_num":
                countList = staList.stream().map(StatisticsDaily::getVideoViewNum).collect(Collectors.toList());
                break;
            case "course_num":
                countList = staList.stream().map(StatisticsDaily::getCourseNum).collect(Collectors.toList());
                break;
            default:
                break;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("dataList", dateList);
        map.put("countList", countList);
        return map;
    }
}
